<?php

namespace app\savant\controller;

use app\common\enums\ResultEnum;
use app\common\exception\ZeduException;
use app\common\model\UserCategory;
use app\common\utils\ResultVOUtil;
use think\Request;
use think\facade\Log;

/**
 * @author zi-m.cn
 *  用户分类管理
 */
class Cate extends Base
{
    /**
     * 文章分类列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where[] = ['uid','=',session('savant.pid')];
        
        try {
            $model = UserCategory::where($where)
                                ->field('pid,id,cate_name')
                                ->order('pid,id,cate_name','asc')
                                ->group('pid,id,cate_name')
                                ->select();

        }catch (\Exception $e){
            Log::record('[分类管理]列表:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::ERROR);
            
        }
        return ResultVOUtil::success($model);
        
    }

    /**
     * 查询父级分类.
     *
     * @return \think\Response
     */
    public function path($pid=0)
    {
        $where['uid'] = session('savant.pid');
        $where['pid'] = $pid;
        try {
            $model = UserCategory::where($where)
                                ->field('pid,id,cate_name')
                                ->select();
            
        } catch (\Exception $e) {
            Log::record('[分类管理]父级:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
        return ResultVOUtil::success($model);
    }

    /**
     * 新增分类
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->param()){
            $model = new UserCategory();
            $model->uid = session('savant.pid');
            try {
                $model->allowField(true)->save($request->param());
                
            } catch (\Exception $e) {
                Log::record('[分类管理]新增:'.$e->getMessage(),'error');
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
            
            return ResultVOUtil::success('');

            
        }else{
            throw new ZeduException(ResultEnum::PARAM_ERROR);
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->param()){
            try {
                $model = UserCategory::get($id);
                if(empty($model)){
                    return ResultVOUtil::error(ResultEnum::PARAM_ERROR['code'], '分类不存在');
                }else{
                    $model->cate_name = $request->param('cateName');
                    if($model->save()){
                        return ResultVOUtil::success('');
                        
                    }else{
                        return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
                    }
                }
                
            } catch (\Exception $e) {
                Log::record('[分类管理]新增:'.$e->getMessage(),'error');
                return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
            }
            
        }else{
            throw new ZeduException(ResultEnum::PARAM_ERROR);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $model = UserCategory::get(['pid'=>$id,'uid'=>session('savant.pid')]);
            if(empty($model)){
                $delete = UserCategory::where(['id'=>$id,'uid'=>session('savant.pid')])->delete();
                if($delete){
                    return ResultVOUtil::success('');
                }else{
                    return ResultVOUtil::error(ResultEnum::DEL_ERROR);
                }
                
            }else{
                return ResultVOUtil::error(ResultEnum::DEL_ERROR['code'], '该分类下有子分类');  
            }
        } catch (\Exception $e) {
            Log::record('[分类管理]删除:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
           
    }
}
